<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;
use think\Db;

/**
 * 产品类型
 * @package app\admin\model
 */
class PtypeModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'e_product_type';
	/**
     * 获取产品类型列表
     * @param int $page 当前分页
	 * @param int $export 是否导出
     * @param string $keyword 搜索关键字
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getPtypeList($input)
	{
		$query = Db::name($this->table)->where('type_status','in','[0,1]');
		$page = isset($input['page']) ? $input['page'] : 1;
		$export = isset($input['export']) ? $input['export'] : 0;
		$order_field = isset($input['order_field']) ? $input['order_field'] : '';
		$order_value = isset($input['order_value']) ? $input['order_value'] : 'desc';
		if(isset($input['keyword']) && !empty($input['keyword'])){
			$query = $this->where('type_code|type_name','like','%'.$input['keyword'].'%');
		}
		$order_arr = ['id'=>'desc'];
		if(!empty($order_field)){
			$order_arr = [$order_field=>$order_value];
		}
		if($export){
			$list = $query->order('id desc')->select();
			return [
				'list'=>$list,
				'page'=>array()
			];
		}else{
			$list = $query->order('id desc')->paginate(config('paginate.list_rows'));
			$page = $list->render();
			return [
				'list'=>$list->toArray(),
				'page'=>$page
			];
		}
	}
	/**
     * 获取产品类型获取尺寸
     * @param array $where 条件
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function getSizeByType($where)
	{
		return Db::name('e_product_type_size')->where($where)->find();
	}
	
	/**
     * 更新产品类型、尺寸的关系
	 * @param input $array 数组
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
	public function insertTypeSize($input)
	{
		$size_id_arr = explode('|',$input['size_id']);
		$type_id = $input['type_id'];
		$where = array();
		$where['type_id'] = $type_id;
		$where['rela_status'] = 0;
		$size_rela = Db::name('e_product_type_size')->where($where)->select();
		if(!empty($size_rela)){
			$new_size_id_arr = array_flip($size_id_arr);
			foreach($size_rela as $_s_r){
				foreach($size_id_arr as $_s){
					if($_s_r['size_'.strtolower($_s)] == 1){
						unset($new_size_id_arr[$_s]);
					}
				}
			}
			if(!empty($new_size_id_arr)){
				$update_data = array();
				foreach($new_size_id_arr as $_n_k=>$_n_s){
					$update_data['size_'.strtolower($_n_k)] = 1;
				}
				return Db::name('e_product_type_size')->where('type_id',$type_id)->update($update_data);
			}else{
				return true;
			}
		}else{
			if(!empty($size_id_arr)){
				$insert_data = array();
				$insert_data['type_id'] = $type_id;
				foreach($size_id_arr as $_s){
					$insert_data['size_'.strtolower($_s)] = 1;
					$insert_data['create_time'] = time();
					$insert_data['update_time'] = time();
				}
				return Db::name('e_product_type_size')->insert($insert_data);
			}else{
				return true;
			}
		}
	}
}
